package com.momalib.ning2_nocode.services.impl;

import com.momalib.ning2_nocode.beans.*;
import com.momalib.ning2_nocode.config.NncConfig;
import com.momalib.ning2_nocode.services.ConfigParseService;
import com.momalib.ning2_nocode.utils.CommonFileUtil;
import com.momalib.ning2_nocode.utils.CommonUtils;
import com.momalib.ning2_nocode.utils.NncConfUtil;
import com.momalib.ning2_nocode.utils.YamlUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class ConfigParseServiceImpl implements ConfigParseService {

    @Autowired
    NncConfUtil nncConfUtil;

    @Override
    public NncProjectConfig analyseProjectConfig(String rootPath) {
        String defaultProjectConfigName = "project_config.yaml";
//        System.out.println(rootPath);
        File rootConfigFile = new File(rootPath + "/" + defaultProjectConfigName);
        if (!rootConfigFile.exists()) {
            System.out.println("文件不存在1");
            return null;
        }

        NncProjectConfig projectRoot = YamlUtil.parseConfig(rootConfigFile, new NncProjectConfig());

        String deployPath = projectRoot.getDeployConfigPath();
        String objectsPath = projectRoot.getObjectsPath();
        String portalsPath = projectRoot.getPortalsPath();
        String serversPath = projectRoot.getServersPath();

        List<String> objectPaths = CommonFileUtil.getFileFromPath(nncConfUtil.convertPath(rootPath, objectsPath));
        List<NncObjectInfo> objects = new ArrayList<>();
        for (String objectPath: objectPaths) {
            File objectFile = new File(objectPath);
            if (objectFile.exists() && objectFile.isFile()) {
                objects.add(YamlUtil.parseConfig(objectFile, new NncObjectInfo()));
            }
        }
        projectRoot.setObjects(objects);

        List<String> portalPaths = CommonFileUtil.getFileFromPath(nncConfUtil.convertPath(rootPath, portalsPath));
        List<NncPortalInfo> portals = new ArrayList<>();
        for (String portalPath: portalPaths) {
            File portalFile = new File(portalPath);
            if (portalFile.exists() && portalFile.isFile()) {
                portals.add(YamlUtil.parseConfig(portalFile, new NncPortalInfo()));
            }
        }
        projectRoot.setPortals(portals);

        List<String> serverPaths = CommonFileUtil.getFileFromPath(nncConfUtil.convertPath(rootPath, serversPath));
        List<NncServerConfig> servers = new ArrayList<>();
        for (String serverPath: serverPaths) {
            File serverFile = new File(serverPath);
            if (serverFile.exists() && serverFile.isFile()) {
                servers.add(YamlUtil.parseConfig(serverFile, new NncServerConfig()));
            }
        }
        projectRoot.setServers(servers);

        File deployFile = new File(nncConfUtil.convertPath(rootPath, deployPath));
        if (deployFile.exists() && deployFile.isFile()) {
            projectRoot.setDeployConfig(YamlUtil.parseConfig(deployFile, new NncDeployConfig()));
        }

        return projectRoot;
    }

    @Override
    public void handleProjectBuild(NncProjectConfig projectConfig) {
        List<NncServerConfig> serverConfigList = projectConfig.getServers();
        if (serverConfigList != null && serverConfigList.size() > 0) {
            for (NncServerConfig serverConfig: serverConfigList) {
                handleServerBuild(serverConfig);
            }
        }
    }

    @Override
    public void handleServerBuild(NncServerConfig serverConfig) {
//        NncServerConfig.NncServerSource serverSource = serverConfig.getSource();

    }

}
